<?php
/**
 * @link https://ym2.cleverstone.top
 * @copyright Copyright (c) 2020 Yii Manager Software LLC
 */

namespace common\helper;

use common\models\AppLogs;
use common\models\EmailRecord;
use common\models\SmsRecord;
use Yii;
use yii\base\UserException;

/**
 * 应用
 * @author cleverstone
 * @since ym2.0
 */
class App extends \builder\helper\App
{
    /**
     * 记录调试日志
     * @param string $category 种类
     * @param array $message 信息
     * @throws UserException
     */
    public static function logDebug($category, array $message)
    {
        static::log('DEBUG', $category, $message);
    }

    /**
     * 记录信息日志
     * @param string $category 种类
     * @param array $message 信息
     * @throws UserException
     */
    public static function logInfo($category, array $message)
    {
        static::log('INFO', $category, $message);
    }

    /**
     * 记录警告日志
     * @param string $category 种类
     * @param array $message 信息
     * @throws UserException
     */
    public static function logWarn($category, array $message)
    {
        static::log('WARNING', $category, $message);
    }

    /**
     * 记录错误日志
     * @param string $category 种类
     * @param array $message 信息
     * @throws UserException
     */
    public static function logError($category, array $message)
    {
        static::log('ERROR', $category, $message);
    }

    /**
     * 记录应用日志
     * @param string $level 日志等级, DEBUG、INFO、WARNING、ERROR
     * @param string $category 种类
     * @param array $message 信息
     * @throws UserException
     */
    public static function log($level, $category, $message)
    {
        if (PHP_SAPI == 'cli') {
            $prefix = '[ CLI ] [ - ] [ ' . PHP_OS . ' ]';
        } else {
            $request = Yii::$app->request;
            $clientIP = $request->userIP ?: '-';
            $os = Http::getClientOs();
            $prefix = '[ CGI-FCGI ] [ ' . $clientIP . ' ] [ ' . $os . ' ]';
        }

        $model = new AppLogs();
        $model->level = $level;
        $model->category = $category;
        $model->prefix = $prefix;
        $model->message = var_export($message, true);

        if (!$model->save()) {
            Yii::error($model->error, __METHOD__);
        }
    }

    /**
     * 校验邮箱验证码
     * @param string $receiveEmail 接收邮箱
     * @param string $code 待校验码
     * @return bool
     */
    public static function verifyEmailCode($receiveEmail, $code)
    {
        $data = EmailRecord::query(['code', 'created_at'])
            ->where(['receive_email' => $receiveEmail])
            ->orderBy(['id' => SORT_DESC])
            ->one();
        if (!empty($data)) {
            $createTime = strtotime($data['created_at']);
            if (time() - $createTime < 300 && !strcmp($data['code'], $code)) {
                return true;
            }
        }

        return false;
    }

    /**
     * 校验短信验证码
     * @param string $receiveMobile 接收手机号
     * @param string $code 待校验码
     * @return bool
     */
    public static function verifySmsCode($receiveMobile, $code)
    {
        $data = SmsRecord::query(['code', 'created_at'])
            ->where(['receive_email' => $receiveMobile])
            ->orderBy(['id' => SORT_DESC])
            ->one();
        if (!empty($data)) {
            $createTime = strtotime($data['created_at']);
            if (time() - $createTime < 300 && !strcmp($data['code'], $code)) {
                return true;
            }
        }

        return false;
    }
}